package com.shyroke.daydayzhuanapi.mapper;

import com.shyroke.daydayzhuanapi.dto.AdminProjectPartDto;
import com.shyroke.daydayzhuanapi.dto.AdminProjectPartSaveDto;
import com.shyroke.daydayzhuanapi.entity.ProjectPartEntity;
import com.shyroke.daydayzhuanapi.entity.ProjectPartTypeEntity;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProjectPartMapper {
    @Autowired
    private ModelMapper modelMapper;

    public AdminProjectPartDto entityToAdminDto(ProjectPartEntity entity) {
        AdminProjectPartDto dto = modelMapper.map(entity,AdminProjectPartDto.class);
        if(entity.getType() != null){
            dto.setTypeName(entity.getType().getDescription());
            dto.setType(entity.getType().getType());
            dto.setTypeId(entity.getType().getId());
        }
        return dto;
    }

    public ProjectPartEntity dtoToEntity(AdminProjectPartSaveDto dto) {
        ProjectPartEntity entity = modelMapper.map(dto, ProjectPartEntity.class);
        entity.setUsed(false);
        if(dto.getTypeId() != null){
            ProjectPartTypeEntity type = new ProjectPartTypeEntity();
            type.setId(dto.getTypeId());
            entity.setType(type);
        }
        return entity;
    }
}
